package cn.agiledata.bank.myspace.action;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.account.service.IAccountConst;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.AccountLimit;
import cn.agiledata.bank.myspace.form.AcctLimitForm;
import cn.agiledata.bank.myspace.service.IMySpaceService;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class AcctLimitAction extends AbstractMySpaceAction implements IAccountConst{
	
	protected IMySpaceService getMySpaceService() throws IPBException {
		return (IMySpaceService) IPBAppFactory.getObj(IMySpaceService.MGR_ID);
	}
	
    public ActionForward toAcctLimit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IPBException, Exception {
    	TransactionTemplate tt = (TransactionTemplate)IPBAppFactory.getObj("transactionTemplate");
    	AccountSummary accountSummary = (AccountSummary) request.getSession().getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
    	Account account = getMySpaceService().getAccount(accountSummary.getAccountId()+"");
    	NumberFormat nf = new DecimalFormat("#0.00#");
    	AccountLimit al = tt.getAcctlimit(account);
 //   	Acctlimit al = tt.getAcctlimit(account);
 //   	String eachlimit = nf.format(al.getEachlimit())+"";
    	String alllimit = nf.format(al.getAlllimit())+"";
    	AccountLimit all = getMySpaceService().getAcctlimitByAcctGrading(account);
//    	String maxeach =nf.format(all.getEachlimit())+"";
    	String maxall = nf.format(all.getAlllimit())+"";
    	AcctLimitForm alform = (AcctLimitForm)form;
    	alform.setAlllimit(alllimit);
//    	alform.setEachlimit(eachlimit);
    	alform.setMaxAlllimit(maxall);
 //   	alform.setMaxEachlimit(maxeach);
 //   	alform.setMaxtranamount(all.getTranamount().intValue()+"");
 //   	alform.setTranamount(al.getTranamount().intValue()+"");
    	return(mapping.findForward("access"));
    }
    
    public ActionForward acctLimit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws IPBException, Exception {
    	AcctLimitForm aform = (AcctLimitForm)form;
    	AccountSummary accountSummary = (AccountSummary) request.getSession().getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
    	Account account = getMySpaceService().getAccount(accountSummary.getAccountId()+"");
//    	if (StringUtils.isEmpty(aform.getEachlimit()) ){
//    		aform.setErrMsg("请输入您要设置的单笔交易金额限制");
//    		return mapping.findForward("limitfailed");
//    	}
    	if(StringUtils.isEmpty(aform.getAlllimit())){
    		aform.setErrMsg("请输入您要设置的单天交易金额限制");
    		return mapping.findForward("limitfailed");
    	}
    	AccountLimit al = new AccountLimit();
    	//getMySpaceService().setSumAmmount(1+"");
  //  	Groups group = 
 //   	Grading grading = account.getGrading();
    	AccountLimit defaultAL = getMySpaceService().getAcctlimitByAcctGrading(account);
    	double alllimit = new Double(aform.getAlllimit()).doubleValue();
//    	double eachlimit = new Double(aform.getEachlimit()).doubleValue();
//    	double tranamount = new Double(aform.getTranamount()).doubleValue();
    	if(alllimit<=0){
    		aform.setErrMsg("您设置的金额不得小于等于0");
    		return mapping.findForward("limitfailed");
    	}
//    	if(eachlimit > alllimit){
//    		aform.setErrMsg("您设置的单笔交易金额大于单天交易金额");		
//    		return mapping.findForward("limitfailed");
//    	}
    	if(alllimit>defaultAL.getAlllimit().doubleValue() ){
    		aform.setErrMsg("您不能设置高于您级别的金额限制");
    		return mapping.findForward("limitfailed");
    	}
    	boolean isNew = false;
    	AccountLimit alt = getMySpaceService().getAcctlimitByAcctId(accountSummary.getAccountId()+"");
    	if(alt == null){
    		isNew = true;
    	}else{al = alt;}
    	setAcctLimit(aform,request,al,isNew);
    	al.setGroups(getMySpaceService().getGroupByAccount(account, Constant.GROUP_TYPE_D));
 //   	al.setGrading(grading);
    	
    	if(isNew){

    		getMySpaceService().saveAcctLimit(al);
    	}else{
    		//al.setId(defaultAL.getId());
    		getMySpaceService().updateAcctLimit(al);
    	}
    	return mapping.findForward("access");
    }
    
    private void setAcctLimit(AcctLimitForm aform,HttpServletRequest request,AccountLimit al,boolean isNew)throws IPBException{
    	AccountSummary accountSummary = (AccountSummary) request.getSession().getAttribute(Constant.SESSION_ACCOUNT_SUMMARY);
    	Double alllimit = new Double(aform.getAlllimit());
//    	Double eachlimit = new Double(aform.getEachlimit());
 //   	Double tranamount = new Double(aform.getTranamount());
    	Account account = getMySpaceService().getAccount(accountSummary.getAccountId()+"");
    	getMySpaceService().getAcctlimitByAcctGrading(account);
    	al.setAlllimit(alllimit);
 //   	al.setEachlimit(eachlimit);
 //   	al.setTranamount(tranamount);
    	if(isNew){
    	/*al.setCreateTime(new Date());
    	al.setCreateUser(accountSummary.getAccountId()+"");
    	}
    	al.setUpdateTime(new Date());
    	al.setUpdateUser(accountSummary.getAccountId()+"");*/
    	WeatherUtil.fillCreateAndUpdateInfo(al, account.getId().toString());
    	al.setLimitType("2");
    	al.setAccount(account);
    	}
    }
}